#! /usr/bin/env zsh
base=$HOME/.xmonad
wall=$base/wallpaper.png
wallong=$base/wallpaper-long.png
lock=$base/lock.png
shPath=$base/sh

# Somehow know a monitor's name
get_ext_monitor_name() {
    monitor_info=$(hwinfo --monitor | grep Model | head -1)
    cut_ind=$(expr index $monitor_info "\"")
    ext_monitor=${monitor_info:$cut_ind}
    ext_monitor=${ext_monitor%"\""}
    echo $ext_monitor
}

restart_apps() {
    pkill polybar
    wait $!
    pkill warpd
    wait $!
    pkill picom
    wait $!
    pkill deadd-notification-center
    wait $!

    $shPath/polybar.sh &
    warpd
    deadd-notification-center &
}

auto() {
    xrandr --output HDMI1 --off eDP1 --off
    wait $!
    xrandr --auto

    feh --bg-fill $wall
    ln -sf $wall $lock

    restart_apps
    wait $!

    picom --config $base/picom.conf &
}

extend() {
    source $base/monitor_profiles/"$(get_ext_monitor_name)"

    if ! [ $? -eq 0 ]; then
        xrandr --output HDMI1 --auto --right-of eDP1
    fi

    echo $CHANGE_WALL
    if [ $CHANGE_WALL ]; then
        feh --bg-fill $wallong
        ln -sf $wallong $lock
    else
        feh --bg-fill $wall
        ln -sf $wall $lock
    fi
    CHANGE_WALL=false

    restart_apps
    picom --vsync &
}

case $1 in
    "-a") auto   ;;
    "-e") extend ;;
esac
